package com.sharemob.tinchat.base;

import java.util.ArrayList;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.sharemob.tinchat.R;

public class ListAdapterForPet extends BaseAdapter {


	ArrayList<PetItem> list = new ArrayList<PetItem>();

	@Override
	public int getCount()
	{
		return list.size();
	}

	@Override
	public Object getItem(int position)
	{
		return list.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder viewHolder = null;
		if (convertView == null)
		{
			viewHolder = new ViewHolder();

			convertView = View.inflate(parent.getContext(),R.layout.layout_pet_info, null);
			findViews(viewHolder, convertView);
			
			convertView.setTag(viewHolder);
		}
		else
		{
			
			viewHolder = (ViewHolder) convertView.getTag();
		}
		setView(viewHolder, position);
		return convertView;
	}

	private void setView(final ViewHolder viewHolder, int position)
	{
		PetItem item = list.get(position);
		viewHolder.itemIcon.setBackgroundResource(item.getIcon());
		viewHolder.itemName.setText(item.getName());
		viewHolder.itemLevel.setText(String.valueOf(item.getLevel()));

	}

	public void setAdapter(ArrayList<PetItem> list)
	{
		this.list = list;
	}

	private void findViews(ViewHolder viewHolder, View convertView)
	{
		viewHolder.itemIcon = (ImageView) convertView.findViewById(R.id.listview_pet_item_icon);
		viewHolder.itemName = (TextView) convertView.findViewById(R.id.listview_pet_item_name);
		viewHolder.itemLevel = (TextView) convertView.findViewById(R.id.listview_pet_item_level);
	}

	protected final class ViewHolder
	{
		ImageView itemIcon;
		TextView itemName;
		TextView itemLevel;
		
	}

}
